package com.dys.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @className: com.dys.servlet.MyServlet01
 * @description: Servlet的生命周期
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/09/19 20:08
 */
public class MyServlet03 extends HttpServlet {
    // 存在线程安全问题
    int num = 1;

    public MyServlet03() {
        System.out.println("Servlet实例化");
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void init() throws ServletException {
        System.out.println("Servlet初始化");
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Servlet执行服务" + (num++));
    }

    @Override
    public void destroy() {
        System.out.println("Servlet销毁");
    }
}
